package cn.edu.zucc.usedcarsales.control;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.edu.zucc.usedcarsales.itf.IVehicleBrandManager;
import cn.edu.zucc.usedcarsales.model.VehicleBrand;
import cn.edu.zucc.usedcarsales.model.VehicleType;
import cn.edu.zucc.usedcarsales.util.DBUtil;

public class VehicleBrandManager implements IVehicleBrandManager {

	@Override
	public List<VehicleBrand> getAllBrand() {
		// TODO Auto-generated method stub
		List<VehicleBrand> brandList = new ArrayList<VehicleBrand>();
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("select * from VehicleBrand");
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				VehicleBrand vt = new VehicleBrand(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5));
				brandList.add(vt);
			}
			return brandList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return brandList;
		}
	}

	@Override
	public void addBrand(VehicleBrand brand) {
		// TODO Auto-generated method stub
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("insert into VehicleBrand (BrandName,BrandCountry,BrandIntroduction,BrandRegTime) values(?,?,?,?)");
			pst.setString(1, brand.getBrandName());
			pst.setString(2, brand.getBrandCountry());
			pst.setString(3, brand.getBrandIntroduction());
			pst.setString(4, brand.getBrandRegTime());
			pst.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void deleteBrand(int id) {
		// TODO Auto-generated method stub
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("delete from VehicleBrand where BrandID = ?");
			pst.setInt(1, id);
			pst.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public VehicleBrand getBrand(int id) {
		// TODO Auto-generated method stub
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("select * from  VehicleBrand where BrandID=?");
			pst.setInt(1, id);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				VehicleBrand vt = new VehicleBrand(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5));
				return vt;
			}
			return null;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public void updateBrand(VehicleBrand brand) {
		// TODO Auto-generated method stub
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("update VehicleBrand set BrandName=?,BrandCountry=?,BrandIntroduction=?,BrandRegTime=? where BrandID=?");
			pst.setString(1, brand.getBrandName());
			pst.setString(2, brand.getBrandCountry());
			pst.setString(3, brand.getBrandIntroduction());
			pst.setString(4, brand.getBrandRegTime());
			pst.setInt(5, brand.getBrandID());
			pst.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public int getBrandIdByName(String name) {
		// TODO Auto-generated method stub
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("select * from VehicleBrand where BrandName=?");
			pst.setString(1,name);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				return rs.getInt("BrandID");
			}
			return 0;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}
	
	
	
}
